package com.example.supernote.ui.components

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties
import com.example.supernote.R

/**
 * 统一的对话框样式（先看 `footer` 参数的介绍）
 *
 * @param modifier
 * @param title 对话框的标题
 * @param cancelButtonText 取消按钮的文本，默认为 "取消"
 * @param confirmButtonText 确认按钮的文本，默认为 "确认"
 * @param onCancel 取消按钮的回调函数
 * @param onConfirm 确认按钮的回调函数
 * @param body 对话框的中部，默认为空。可以自定义
 * @param footer 对话框的底部，默认有一个确认按钮和取消按钮，也可以自定义。
 *              自定义时所有与按钮相关的参数失效，可直接忽略。需要在自定义的函数中手动指定。
 *              可参考 `TextAiPlus.kt 第 71 行`
 */
@Composable
fun UniformDialog(
    modifier: Modifier = Modifier,
    title: String = "",
    cancelButtonText: String = stringResource(R.string.note_cancel),
    confirmButtonText: String = stringResource(R.string.note_confirm),
    onCancel: () -> Unit = {},
    onConfirm: () -> Unit = {},
    body: @Composable () -> Unit = {},
    footer: @Composable () -> Unit = {
        Row(
            modifier = Modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.SpaceBetween,
        ) {
            Spacer(modifier = Modifier.weight(1f))
            UniformButton(onClick = onConfirm, text = confirmButtonText)
            Spacer(modifier = Modifier.width(5.dp))
            UniformButton(onClick = onCancel, text = cancelButtonText)
        }
    }
) {
    Dialog(
        onDismissRequest = onCancel,
        properties = DialogProperties(
            dismissOnBackPress = true, dismissOnClickOutside = false
        )
    ) {
        Box(
            modifier = modifier.background(MaterialTheme.colorScheme.background),
            contentAlignment = Alignment.Center
        ) {
            Column(
                modifier = Modifier.padding(16.dp),
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.spacedBy(16.dp, Alignment.CenterVertically)
            ) {
                Text(
                    text = title,
                    style = MaterialTheme.typography.titleLarge,
                    color = MaterialTheme.colorScheme.onBackground
                )
                body()
                footer()
            }
        }
    }
}